<?php

namespace backend\models\service;

use Yii;
use yii\data\Sort;
use yii\data\ActiveDataProvider;
use backend\models\odm\RechargeOdm;
use yii\data\ArrayDataProvider;

class Bill extends RechargeOdm {

    private $_query;
    public $start;
    public $end;
    
    /**
     * @inheritdoc
     */
    public function rules() {
        return array_merge(parent::rules(), [
            [['start','end'], 'string'],
        ]);
    }

    public function init() {
        parent::init();
        $this->_query = static::find();
    }

    /**
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function getQuerySearch($params) {
        $this->load($params);
        $this->_query->andFilterWhere([
            'id' => $this->id,
            'status' => $this->status,
            'pay_type' => $this->pay_type,
        ]);
        if($this->start){
            $this->_query->andFilterWhere(['>=', 'pay_time', $this->start]);
        }
        
        if($this->end){
            $this->_query->andFilterWhere(['<=', 'pay_time', $this->end]);
        }
//        $this->time($this->_query, [
//            'pay_time',
//        ]);
        return $this;
    }

    public function getList() {
        $query = $this
            ->getQuerySearch(Yii::$app->request->queryParams)
            ->getQuery();

        return new ActiveDataProvider([
            'query' => $query,
        ]);
    }

    /**
     *
     * 时间的搜索
     *
     * @param $query , array $colmns
     *
     * @return $query
     * */
    private function time(&$query, $colmns) {
        foreach ($colmns as $key => $value) {
            if ($this->$value != '') {
                $query->andWhere(['between', $value, strtotime($this->$value), strtotime($this->$value) + 86400]);
            } else {
                $this->$value = null;
            }
        }
        return $query;
    }

    public function getQuery() {
        return $this->_query;
    }
    
    /**
     * 获取充值金额
     * @param type $type
     * 1、今日充值金额 2、昨日种子金额 3、本周充值金额 4、本月充值金额 5、累计充值金额
     */
    public static function getReChargeMoney($type){
        if($type == 1){
            $s = date('Y-m-d 00:00:00');
            $e = date('Y-m-d 23:59:59');
            $sql = "select sum(pay_money) as pay_money ,count(id) as n from xs_recharge where pay_time >= '$s'  and pay_time <= '$e'  and status = 1";
            $res1 = Yii::$app->db->createCommand($sql)->queryOne();
            return $res1;
        }elseif($type == 2){
            $s = date('Y-m-d 00:00:00',strtotime('-1 day'));
            $e = date('Y-m-d 23:59:59',strtotime('-1 day'));
            $sql2 = "select sum(pay_money) as pay_money ,count(id) as n from xs_recharge where pay_time >= '$s'  and pay_time <= '$e'  and status = 1";
            $res2 = Yii::$app->db->createCommand($sql2)->queryOne();
            return $res2;
        }elseif($type == 3){
            $times = self::getTimeBetween(2);
            $s = $times['start'];
            $e = $times['end'];
            $sql3 = "select sum(pay_money) as pay_money ,count(id) as n from xs_recharge where pay_time >= '$s'  and pay_time <= '$e'  and status = 1";
            $res3 = Yii::$app->db->createCommand($sql3)->queryOne();
            return $res3;
        }elseif($type == 4){
            $times = self::getTimeBetween(3);
            $s = $times['start'];
            $e = $times['end'];
            $sql4 = "select sum(pay_money) as pay_money ,count(id) as n from xs_recharge where pay_time >= '$s'  and pay_time <= '$e'  and status = 1";
            $res4 = Yii::$app->db->createCommand($sql4)->queryOne();
            return $res4;
        }elseif($type == 5){
            $s = date('0000-00-00 00:00:00',strtotime('-30 day'));
            $e = date('Y-m-d 23:59:59');
            $sql5 = "select sum(pay_money) as pay_money ,count(id) as n from xs_recharge where pay_time >= '$s'  and pay_time <= '$e'  and status = 1";
            $res5 = Yii::$app->db->createCommand($sql5)->queryOne();
            return $res5;
        }
    }
    
    
    public function getDatas($s = '' ,$e = '',$daily_type = 1){
        if($daily_type > 0){
            switch($daily_type){
                case 1:
                    $times = self::getTimeBetween(2);
                    break;
                case 2:
                    $times = self::getTimeBetween(3);
                    break;
                case 3:
                    $times = array(
                        'start' => '0000-00-00 00:00:00',
                        'end' => date('Y-m-d H:i:s'),
                    );
                    break;
                default:
                    break;
            }
            $s = $times['start'];
            $e = $times['end'];
            $sql = "select date_format(pay_time,'%Y-%m-%d') as day, count(1) as n,sum(pay_money) as total_money from xs_recharge where status = 1 and "
                . "pay_time >= '$s' and pay_time<= '$e'   group by date_format(pay_time,'%Y-%m-%d') order by day desc";
        }else{
            $sql = "select date_format(pay_time,'%Y-%m-%d') as day, count(1) as n,sum(pay_money) as total_money from xs_recharge where status = 1 and "
                . "pay_time >= '$s' and pay_time<= '$e'   group by date_format(pay_time,'%Y-%m-%d') order by day desc";
        }
        $res = Yii::$app->db->createCommand($sql)->queryAll();
        $data['dataProvider'] = new ArrayDataProvider(array(
            'allModels'=> $res,
            'pagination' => [
                'defaultPageSize' => 10,
            ]
        ));
        return $data;
    }
    
    /**
     * 获取时间区间
     */
    private static function getTimeBetween( $type )
    {
        $now        = date( 'Y-m-d H:i:s' );
        $year       = date('Y');
        $month_n    = date('n');
        $day_j      = date('j');
        $week_day   = date('w');
        switch( $type ){
            case 1:  //日榜
                $yestoday = date( 'Y-m-d 00:00:00' , strtotime('-1 days') );
                $tomorrow = date( 'Y-m-d 00:00:00' , strtotime('+1 days') );
                $times['start'] = $yestoday;
                $times['end'] = $tomorrow;
                break;
            case 2:  //周榜
                if( $week_day == 0 || $week_day == 1 ){
                    $times['start'] = date( 'Y-m-d 00:00:00' , strtotime('-7 days') );
                    $times['end'] = $now;
                }else{
                    $times['start'] = date( 'Y-m-d 00:00:00' , time() - ( $week_day - 1 ) * 24 * 3600 );
                    $times['end'] = $now;
                }
                break;
            case 3:  //月榜
                if( $day_j == 1 ){
                    if( $month_n == 1 ){
                        $last_year = $year - 1;
                        $times['start'] = $last_year.'-12-01 00:00:00';
                        $times['end'] = $now;
                    }else{
                        $last_month = date( 'Y-m-01 00:00:00' , strtotime('-1 month') );
                        $times['start'] = $last_month;
                        $times['end'] = $now;
                    }
                }else{
                    $times['start'] = date( 'Y-m-01 00:00:00' );
                    $times['end'] = $now;
                }
                break;
            default :
                break;
        }
        return $times;
    }
}
